package cst.zyfypt_917cqy.zyfypt917cqy;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by ZHANG on 2017/5/8.
 */

public class ResModel <T> implements AmicoolIface {
    private Retrofit retrofit;
    private ResService service;

    private static final  String BASE_UAL="http://amicool.neusoft.edu.cn/";

    public ResModel(String type) {
        if(type.equals("gson")){
            retrofit=new Retrofit.Builder().baseUrl(BASE_UAL).addConverterFactory(GsonConverterFactory.create()).build();
        }
        else{
            retrofit=new Retrofit.Builder().baseUrl(BASE_UAL).addConverterFactory(ScalarsConverterFactory.create()).build();
        }
        service=retrofit.create(ResService.class);
    }


    @Override
    public void login(String u, String p, final AmicoolListener l) {
        Call<LoginBean> call= service.login(u,p);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l.onFail(t.toString());

            }
        });



    }

    @Override
    public void reg(String u, String p, String t, int roleid, String email) {

    }

    @Override
    public void getList(String mod, int page,  final AmicoolListener l) {
        switch(mod)
        {
            case"video":
                Call<List<VideoBean>> call=service.getVideoList(mod,page,"2ts2j910hv4m270hipde4a67d7"
                );
                call.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());

                    }
                });
                break;
        }

    }

    @Override
    public void getVideoDetail(String mod, int id, String s, AmicoolListener l) {

    }
}
